################################################################################ 
#
# Counterspeech encouraging users to adopt the perspective of minority 
# groups reduces hate speech and its amplification on social media
#
# Tab A1  
#
################################################################################ 


rm(list = ls())

# Set to save results
setsave = T

################################################################################ 
#  LIBRARIES
################################################################################ 

library(plyr)
library(dplyr)
library(tidyr)
library(readr)
library(ggpubr)
library(xtable)
library(estimatr)
library(lubridate)

################################################################################ 
#   DATA AND FOLDER
################################################################################ 

wd = ''
wd_res = paste0(wd, '/results')
wd_data =  paste0(wd, '/data')


data = read.csv(paste0(wd_data, '/dataset_analysis.csv'))
data = data[!(data$user_deleted == "yes"),]

# Create week factors 
data = data %>% mutate(
  datetime = ymd_hms(treated_at),
  day = as.factor(as_date(datetime)),
  week = floor_date(datetime, unit = "week"),   
  week = as.factor(week))

################################################################################ 
#   VARIABLES
################################################################################

outvars = c('orig_tweet_deleted', 'total_tweets_post',
                'tox_dummy_p_key_num', 'tox_dummy_p_key')

outvars_name = c('Xenophobic Tweet Deleted', '# Total Tweets',
                     '# Xenophobic Tweets', 'Xenophobic Tweet Share')

treatments = c('empathy', 'traditional', 'analogical', 'perspective_getting', 'disapproval')
treatments_lab = c( 'Perspective (all)', 'Traditional Perspective Taking',  'Analogical Perspective Taking',
                    'Perspective Getting', 'Disapproval')

# Controls in the PAP
data$account_age_cuts = as.factor(cut(data$account_age, breaks=5))
controls = c("account_age_cuts", 'tox_num_pre', "followers_count",
             "pre_treat_tweet_length", "total_tweets_pre", "friends_count")

# Controls in the PAP + Week fixed effects
controls_maj = c("account_age_cuts", 'tox_num_pre', "followers_count",
                 "pre_treat_tweet_length", "total_tweets_pre", "friends_count", "week")

################################################################################ 
#   Scale variables
################################################################################ 

data[c(outvars)] = lapply(data[c(outvars)], scale)


################################################################################ 
#   Run the models with and without controls
################################################################################ 


MODELS = list()
MODELS_controls = list()
MODELS_controls_maj = list()

n = 1

for (j in 1:length(outvars)){
  for (i in 1:length(treatments)){
    
    # drop missings
    droper = union(which(is.na(data[outvars[j]])), which(data[outvars[j]]==Inf))
    
    # keep the right treatment and control group
    keeper = union(which(data[treatments[i]] == 1),  which(data$group_number == 5)) 
    keeper = setdiff(keeper, droper)
    
    # select rows
    y = data[keeper, outvars[j]]
    d = data[,treatments[i]][keeper]
    ctr = data[keeper, controls]
    ctr_maj = data[keeper, controls_maj]
    
    # regression
    MODELS[[n]] = lm_robust(y ~ d, se_type='HC3')
    MODELS_controls[[n]] = lm_robust(y ~ ., se_type='HC3', data = cbind(d, ctr))
    MODELS_controls_maj[[n]] = lm_robust(y ~ ., se_type='HC3', data = cbind(d, ctr_maj))
    
    print('Done with: ')
    print(n)
    
    n = n+1
    
  }}



################################################################################ 
#   Save results in a container
################################################################################ 


make_container = function(list_to_be_transformed, outvars, outvars_name){
  MOD = list_to_be_transformed
  
  coef = sapply(MOD, function(m) summary(m)$coefficients[2, 1])
  se = sapply(MOD, function(m) summary(m)$coefficients[2, 2])
  pval = sapply(MOD, function(m) summary(m)$coefficients[2, 4])
  sample = sapply(MOD, function(m) nobs(m))
  
  container = cbind.data.frame(yvar = rep(outvars, each=5),
                              yvar_name = rep(outvars_name, each=5),
                               treat = rep(treatments, time=length(outvars)),
                               treatments_lab = rep(treatments_lab, time=length(outvars)),
                               coef = coef, se=se, pval=pval, 
                               samplesize = sample
  )
  return(container)
}


container_unc = make_container(MODELS, outvars, outvars_name)
container_ctr = make_container(MODELS_controls, outvars, outvars_name)
container_ctr_maj = make_container(MODELS_controls_maj, outvars, outvars_name)


if (setsave){write.csv(container_unc, paste0(wd_res, '/tables/main_result_wo_controls.csv'), row.names = F)}
if (setsave){write.csv(container_ctr, paste0(wd_res, '/tables/main_result_w_controls.csv'), row.names = F)}
if (setsave){write.csv(container_ctr_maj, paste0(wd_res, '/tables/main_result_w_controls_maj.csv'), row.names = F)}
